package com.mmlc.meimei.lichengkongjian.Adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.widget.CheckableTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/27.
 */
public class ConditionAdapter<T> extends BaseAdapter {
    private Context mContext;
    private List<T> mList;

    private List<String> selectList;

    public List<String> getSelectList() {
        return selectList;
    }

    public void setSelectList(List<String> selectList) {
        this.selectList = selectList;
    }

    public ConditionAdapter(Context context) {
        mContext = context;
        selectList = new ArrayList<>();
    }

    public ConditionAdapter(List<T> list, Context context) {
        mList = list;
        mContext = context;
        selectList = new ArrayList<>();
    }


    public Context getContext() {
        return mContext;
    }

    public void setContext(Context context) {
        mContext = context;
    }

    public List<T> getList() {
        if (mList == null)
            mList = new ArrayList<>();
        return mList;
    }

    public void setList(List<T> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public String getItem(int position) {
        return (String) mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            CheckableTextView tv = new CheckableTextView(getContext());
            AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                    getContext().getResources().getDimensionPixelSize(R.dimen.x190)
                    , getContext().getResources().getDimensionPixelSize(R.dimen.y80));
            tv.setLayoutParams(layoutParams);
            tv.setGravity(Gravity.CENTER);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP
                    , 12);
            convertView = tv;
        }
        ((CheckableTextView) convertView).setOnCheckedChangeListener(new CheckableTextView.OnCheckedChangeListener() {
            @Override
            public void onChecked(boolean checked) {
                if (checked) {
                    if (!selectList.contains(getItem(position)))
                        selectList.add(getItem(position));
                } else {
                    if (selectList.contains(getItem(position)))
                        selectList.remove(getItem(position));
                }

            }
        });
        ((CheckableTextView) convertView).setText(getItem(position));
        ((CheckableTextView) convertView).setChecked(selectList.contains(getItem(position)));
        return convertView;
    }
}
